<?php
include_once 'checkAdmin.php';
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>会员管理系统</title>
    <style>
        .main {
            width: 100%;
            margin: 0 auto;
            text-align: center
        }

        h2 a {
            margin-right: 15px;
            color: darkcyan;
            text-decoration: none
        }

        h2 a:hover {
            color: crimson;
            text-decoration: underline
        }

        h2 a:last-child {
            margin-right: 0
        }
        .current {
            color: #f8ab0a
        }
        .red{color: red}
        .yellow{background-color: yellow}
        .white{background-color: white}
        .green{background-color: #5c874b}
    </style>
</head>
<body>
<div class="main">
    <?php include_once 'nav.php';?>
    <table width="100%" border="0" align="center">
        <tr>
            <td>
                <table width="100%" border="1" bordercolor="gray" cellspacing="0" cellpadding="10"
                       style="border-collapse: collapse">
                    <tr>
                        <td align="center">序号</td>
                        <td align="center">用户名</td>
                        <td align="center">性别</td>
                        <td align="center">信箱</td>
                        <td align="center">爱好</td>
                        <td align="center">是否管理员</td>
                        <td align="center">操作</td>
                    </tr>
                    <?php
                    include_once 'conn.php';
                    include_once 'page.php';
                    $sql = "select count(id) total from userinfo";
                    $result = mysqli_query($conn,$sql);
                    $info = mysqli_fetch_array($result);
                    $total = $info['total'];  //得到总的记录数
                    $pageSize = 5;  //设置每一页显示多少条数据
                    //获取当前页码
                    $page1 = $_GET['page'] ?? 1;
                    //验证参数是否正确
                    if(!is_numeric($page1) || $page1 <= 0){
                        echo "<script>alert('参数错误');history.back();</script>";
                        exit;
                    }
                    paging($total,$pageSize);
                    $sql = "select * from userinfo order by id desc limit  $firstCount,$pageSize";
                    $result = mysqli_query($conn,$sql);
                    $i = ($page - 1) * $pageSize + 1; //序号初始化
                    while ($info = mysqli_fetch_array($result)){
                        ?>
                        <tr class="white" onmouseover="if(this.className==='white'){this.className='yellow'}" onmouseout="if(this.className==='yellow'){this.className='white'}" onclick="if(this.className === 'green'){this.className='white'}else{this.className='green'}">
                            <td align="center"><?php echo $i; ?></td>
                            <td align="center"><?php echo $info['username']; ?></td>
                            <td align="center"><?php echo $info['sex'] ? '男' : '女'; ?></td>
                            <td align="center"><?php echo $info['email']; ?></td>
                            <td align="center"><?php echo $info['fav']; ?></td>
                            <td align="center"><?php echo $info['admin'] ? '是' : '否'; ?></td>
                            <td align="center">
                                <?php
                                if($info['admin']){
                                    if($info['username'] !== 'admin') {
                                        echo '<a href="setAdmin.php?userID=' . $info['id'] . '&action=0">取消管理员</a>';}
                                    else{echo '<span style="color: grey">取消管理员</span>';}
                                }else{echo '<a href="setAdmin.php?userID='.$info['id'].'&action=1">设置管理员</a>';}
                                if($info['username'] !=='admin'){
                                    echo "　<a href='del.php?id=".$info['id']."'>删除</a>　";
                                }
                                else{
                                    echo"　<span style='color:gray'>删除</span>　";
                                }
                                ?>
                                 修改资料</td>
                        </tr>
                        <?php
                        $i++;
                    }
                    ?>
                </table>
            </td>
        </tr>
        <tr>
            <td align="right"><?php echo $pageNav;?></td>
        </tr>
    </table>

</div>
</body>
</html>
